package com.tianshi.yingxue;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.PutObjectRequest;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.util.Date;
import java.util.List;

@SpringBootTest
public class OssJavaSdkQuickStart {
    @Test
    public void createBucket() {
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI5t7gwJGT9F22fS4Cpb5w";
        String accessKeySecret="a4SXM2eT6j6DWv5W5hOtibiDu7X6wT";
        String bucketName = "yingxx-000";
        OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
        ossClient.createBucket(createBucketRequest);
        ossClient.shutdown();
    }
    @Test
    public void queryBucket() {
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI5t7gwJGT9F22fS4Cpb5w";
        String accessKeySecret="a4SXM2eT6j6DWv5W5hOtibiDu7X6wT";
        String bucketName = "yingxx-000";
        OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        List<Bucket> buckets = ossClient.listBuckets();
        for (Bucket bucket : buckets) {
            System.out.println(bucket.getName());
        }
        ossClient.shutdown();
    }
    @Test
    public void deleteBucket() {
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI5t7gwJGT9F22fS4Cpb5w";
        String accessKeySecret="a4SXM2eT6j6DWv5W5hOtibiDu7X6wT";
        String bucketName = "yingxx-000";
        OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        ossClient.deleteBucket(bucketName);
        ossClient.shutdown();
    }
    @Test
    public void uploadAliyun(){
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI5t7gwJGT9F22fS4Cpb5w";
        String accessKeySecret="a4SXM2eT6j6DWv5W5hOtibiDu7X6wT";
        String bucketName = "yingx-000";
        String objectName = "photo/a.jpg";
        String localFile="C:\\Users\\qq740\\Downloads\\a.jpg";
        OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName,objectName,new File(localFile));
        ossClient.putObject(putObjectRequest);
        ossClient.shutdown();
    }
    @Test
    public void testDownload(){
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI5t7gwJGT9F22fS4Cpb5w";
        String accessKeySecret="a4SXM2eT6j6DWv5W5hOtibiDu7X6wT";
        String bucketName = "yingx-000";
        String objectName = "photo/a.jpg";
        String localFile="C:\\"+objectName;
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            File file = new File(localFile);
            if(!file.getParentFile().exists()){
                file.getParentFile().mkdirs();
            }
            ossClient.getObject(new GetObjectRequest(bucketName,objectName),file);
            System.out.println("文件下载成功，保存路径"+localFile);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            ossClient.shutdown();
        }
    }
    @Test
    public void testDeleteFile(){
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI5t7gwJGT9F22fS4Cpb5w";
        String accessKeySecret="a4SXM2eT6j6DWv5W5hOtibiDu7X6wT";
        String bucketName = "yingx-000";
        String objectName = "photo/a.jpg";
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            if (ossClient.doesObjectExist(bucketName,objectName)) {
                ossClient.deleteObject(bucketName,objectName);
                System.out.println("文件删除成功");
                System.out.println("删除时间"+new Date());
                System.out.println("删除的文件路径"+objectName);
            }else {
                System.out.println("文件不存在，无需删除");
                System.out.println("检查路径"+objectName);
            }
        }catch (Exception e){
            System.out.println("删除失败，OSS服务端错误");
            e.printStackTrace();
        }finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}
